package com.ketaichan.zolon_printer.utils;

import android.os.ConditionVariable;

public class ConditionVariableUtils {
    private long default_lock_timeout = 3000;
    private ConditionVariable mConditionVariable;

    public ConditionVariableUtils() {
        mConditionVariable = new ConditionVariable();
    }

    public ConditionVariableUtils(int lockTimeout) {
        default_lock_timeout = lockTimeout;
        mConditionVariable = new ConditionVariable();
    }

    public void lock() { // 阻塞当前线程，直到条件为open
        // 如果是点击了按钮，则先将条件重置，否则block会失效
        mConditionVariable.close();
        // 延时等待3秒
        mConditionVariable.block(default_lock_timeout);
    }

    public void unlock() { // 释放所有阻塞的线程
        mConditionVariable.open();
    }
}